// pages/common/alert/alert.js
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持 
  },
  properties: {
    type: {
      type: String,
      value: 'success'
    },
    text: {
      type: String,
      value: ''
    },
    show: {
      type: Boolean,
      value: false
    },
    time: {
      type: Number,
      value: 3000
    }
  },
  data: {
    showThis: false,
    st: null
  },
  ready() {
    let val
    Object.defineProperty(this.data, 'show', {
      configurable: true,
      enumerable: true,
      set: (value) => {
        val = value
        setTimeout(() => {
          // console.log(value, this.data.type)
            switch (this.data.type) {
              case 'loading':
                clearTimeout(this.data.st)
                this.showLoading(value)
                break;
              default:
                value?this.showAlert():null
            }
        }, 0)
      },
      get: () => {
        return val;
      }
    })
  },
  methods: {
    showAlert() {
      let t = this.data.time
      setTimeout(() => {
        this.setData({
          showThis: true
        })
        clearTimeout(this.data.st)
        let st = setTimeout(() => {
          this.hideLoading()
        }, t ? t : 3000)
        this.setData({
          st
        })
      }, 20)
    },
    showLoading(judge) {
      this.setData({
        showThis: judge,
      })
    },
    hideLoading() {
      this.setData({
        showThis: false,
        show: false
      })
    }
  }
})